#!/bin/bash
#-----------------------------------------------------------------------#
# System security initiate hardening tool for Ubuntu Server.
# WeiyiGeek <master@weiyigeek.top>
# Blog : https://blog.weiyigeek.top
#
# The latest version of my giuthub can be found at:
# https://github.com/WeiyiGeek/SecOpsDev/
# 
# Copyright (C) 2020-2022 WeiyiGeek
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#-------------------------------------------------------------------------#
# Get load averages
LOAD1=$(grep "" /proc/loadavg | awk '{print $1}')
LOAD5=$(grep "" /proc/loadavg | awk '{print $2}')
LOAD15=$(grep "" /proc/loadavg | awk '{print $3}')

# Get free memory
MEMORY_USED=$(free -t -m | grep "Mem" | awk '{print $3}')
MEMORY_ALL=$(free -t -m | grep "Mem" | awk '{print $2}')
MEMORY_PERCENTAGE=$(free | awk '/Mem/{printf("%.2f%"), $3/$2*100}')

# Get system uptime
UPTIME=$(grep "" /proc/uptime | cut -f1 -d.)
UPTIME_DAYS=$(("${UPTIME}"/60/60/24))
UPTIME_HOURS=$(("${UPTIME}"/60/60%24))
UPTIME_MINS=$(("${UPTIME}"/60%60))
UPTIME_SECS=$(("${UPTIME}"%60))

# Get processes
PROCESS=$(ps -eo user=|sort|uniq -c | awk '{print $2 " " $1 }')
PROCESS_ALL=$(echo "${PROCESS}" | awk '{print $2}' | awk '{SUM += $1} END {print SUM}')
PROCESS_ROOT=$(echo "${PROCESS}" | grep root | awk '{print $2}')
PROCESS_USER=$(echo "${PROCESS}" | grep -v root | awk '{print $2}' | awk '{SUM += $1} END {print SUM}')

# Get processors
PROCESSOR_NAME=$(grep "model name" /proc/cpuinfo | cut -d ' ' -f3- | awk '{print $0}' | head -1)
PROCESSOR_COUNT=$(grep -ioP 'processor\t:' /proc/cpuinfo | wc -l)


# Colors
G="\033[01;32m"
R="\033[01;31m"
D="\033[39m\033[2m"
N="\033[0m"

echo -e "\e[01;38;44;5m########################## 安全运维 (Security Operation) ############################\e[0m"
echo -e "${G}Login success.${N} Please execute the commands and operation data carefully.By WeiyiGeek."
echo -e "You are logged in to ${G}$(uname -n)${N}, Login time is $(/bin/date "+%Y-%m-%d %H:%M:%S").\n"
echo -e "[System Info]\n"
echo -e "  SYSTEM    : $(awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release)"
echo -e "  KERNEL    : $(uname -sr)"
echo -e "  UPTIME    : ${UPTIME_DAYS} days ${UPTIME_HOURS} hours ${UPTIME_MINS} minutes ${UPTIME_SECS} seconds"
echo -e "  CPU       : ${PROCESSOR_NAME} (${G}${PROCESSOR_COUNT}${N} vCPU)\n"
echo -e "  MEMORY    : ${MEMORY_USED} MB / ${MEMORY_ALL} MB (${G}${MEMORY_PERCENTAGE}${N} Used)"
echo -e "  LOAD AVG  : ${G}${LOAD1}${N} (1m), ${G}${LOAD5}${N} (5m), ${G}${LOAD15}${N} (15m)"
echo -e "  PROCESSES : ${G}${PROCESS_ROOT}${N} (root), ${G}${PROCESS_USER}${N} (user), ${G}${PROCESS_ALL}${N} (total)"
echo -e "  USERS     : ${G}$(users | wc -w)${N} users logged in"
echo -e "  BASH      : ${G}${BASH_VERSION}${N}\n"

echo -e "[Disk Usage]\n"
mapfile -t DFH < <(df -h -x zfs -x squashfs -x tmpfs -x devtmpfs -x overlay --output=target,pcent,size,used | tail -n+2)
for LINE in "${DFH[@]}"; do
    # Get disk usage
    DISK_USAGE=$(echo "${LINE}" | awk '{print $2}' | sed 's/%//')
    USAGE_WIDTH=$((("${DISK_USAGE}"*60)/100))

    # If the usage rate is <90%, the color is green, otherwise it is red
    if [ "${DISK_USAGE}" -gt 90 ]; then
        COLOR="${R}"
    else
        COLOR="${G}"
    fi

    # Print the used width
    BAR="[${COLOR}"
    for ((i=0; i<"${USAGE_WIDTH}"; i++)); do
        BAR+="="
    done

    # Print unused width
    BAR+=${D}
    for ((i="${USAGE_WIDTH}"; i<60; i++)); do
        BAR+="="
    done
    BAR+="${N}]"

    # Output
    echo "${LINE}" | awk '{ printf("Mounted: %-32s %s / %s (%s Used)\n", $1, $4, $3, $2); }' | sed -e 's/^/  /'
    echo -e "${BAR}" | sed -e 's/^/  /'
done
echo
